package com.lanqiao.guimei.controller;

import com.lanqiao.guimei.dao.UsersDao;
import com.lanqiao.guimei.vo.Page;
import com.lanqiao.guimei.vo.Users;
import com.lanqiao.web.mvc.annotation.Controller;
import com.lanqiao.web.mvc.annotation.RequestMapping;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 韩原
 * @date 2019/8/13  19:12
 */
@Controller
public class UserController {
    Logger logger = Logger.getLogger(UserController.class);
    @RequestMapping("/user/page")
    public String findForPage(HttpServletRequest request, HttpSession session, Users users){


        //创建了一个session作用域 将page的对象先存进去
        Page<Users> usersPage = (Page<Users>) session.getAttribute("usersPage");
        //创建了一个UsersDao对象
        UsersDao usersDao = new UsersDao();

        String pageNo = request.getParameter("pageNo");

        if (usersPage == null || StringUtils.isBlank(pageNo)){
            usersPage = new Page<Users>();
            //得到每页显示的记录数
            usersPage.setPageSize(15);
            //得到当前页号
            usersPage.setPageNo(1);
            //设置查询分页的条件
            usersPage.setQueryObject(users);
            // 统计总记录数
            usersPage.setTotalRow(usersDao.countForPage(usersPage));
        } else {
            //得到当前页号
            usersPage.setPageNo(Integer.valueOf(pageNo));
        }

        session.setAttribute("usersPage", usersPage);

        // 查询列表数据
        List<Users> usersList = usersDao.findForPage(usersPage);
        System.out.println(usersList);
        request.setAttribute("usersList", usersList);
        return "/after/userquery.jsp";
    }
    @RequestMapping("/user/delete")
    public Map userdelete(Users users,HttpServletRequest request){
        System.out.println("进来了吗");
     Boolean b=new UsersDao().deleteuser(users);
        if (b) {
            Map json = new HashMap();
            json.put("result", b);
            return json;
        }else {
            Map json = new HashMap();
            json.put("result", b);
            return json;
        }
    }
    @RequestMapping("/user/update")
    public Map userupdata(Users users,HttpServletRequest request){

        Boolean b=new UsersDao().userupdata(users);
        if (b) {
            Map json = new HashMap();
            json.put("result", b);
            return json;
        }else {
            Map json = new HashMap();
            json.put("result", b);
            return json;
        }
    }

    @RequestMapping("/toLogin")
    public String toLogin(HttpServletRequest request){
        return "redirect:/front/login.jsp";
    }

    @RequestMapping("/toRegister")
    public String toRegister(HttpServletRequest request){
        return "/front/register.jsp";
    }

    //登陆
    @RequestMapping("/userLogin")
    public String userLogin(HttpSession session,Users users){
        Users loginUser = new UsersDao().login(users);
        if (loginUser !=null){
            session.setAttribute("loginUser",loginUser);
            return "redirect:/front/index.jsp";
        }
        return "redirect:/front/login.jsp";
    }

    //注册
    @RequestMapping("/front/user/register")
    public String userRegister(Users users){
        boolean b = new UsersDao().register(users);
        if (b){
            return "redirect:/front/login.jsp";
        }
        return "redirect:/front/test.jsp";
    }

    @RequestMapping("/toIndex")
    public String toIndex(HttpServletRequest request){
        return "redirect:/front/index.jsp";
    }
}
